<template>
  <div class="app-param">
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.native.prevent>
      <el-form-item label="标签名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入标签名称" clearable></el-input>
      </el-form-item>
      <el-form-item :inline="true">
        <el-button type="primary" icon="Search" @click="handleSearch">搜索</el-button>
        <el-button icon="Refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div>
      <el-button type="primary" plain size="small" icon="Plus" @click="handleAdd">新增</el-button>
      <el-button type="danger" plain size="small" icon="Delete" @click="handleDelete">删除</el-button>
    </div>

    <el-table
      :data="tagList"
      v-loading="loading"
      stripe
      ref="multipleTableRef"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="编号" width="55" align="center" />
      <el-table-column prop="name" label="名称" align="center" />
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button size="small" type="text" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageClass">
      <el-pagination
        :hide-on-single-page="true"
        v-model:currentPage="queryParams.current"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryParams.size"
        :pager-count="11"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <el-dialog v-model="visible" :title="title" width="500px" append-to-body destroy-on-close>
      <el-form :model="form" label-width="80px" :rules="rules" @submit.native.prevent>
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入标签名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" size="large" @click="submitForm">确认</el-button>
          <el-button @click="visible = false" size="large">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import type { Tag } from '@/api/types/tag'
import { pageList, add, deleteByIds } from '@/api/tag'

const queryParams = reactive({
  name: undefined,
  current: 1,
  size: 10
})

const tagList = ref<Tag[]>([])

const total = ref(0)

const multipleSelection = ref<number[] | undefined>([])

const multipleSelectionName = ref<string[]>([])

const loading = ref(false)

const visible = ref(false)

const title = ref('')

const form = reactive<Tag>({
  tagId: 0,
  name: '',
  createTime: ''
})

const rules = reactive({})

onMounted(() => {
  loadList()
})

/**
 * 加载数据
 */
const loadList = async () => {
  loading.value = true
  const tagPage = await pageList(queryParams)
  tagList.value = tagPage.records
  total.value = tagPage.total
  loading.value = false
}

/**
 * 查询数据列表
 */
const handleSearch = () => {
  loadList()
}

/**
 * 重置查询条件
 */
const handleReset = () => {
  queryParams.name = undefined
  queryParams.current = 1
  queryParams.size = 10
  loadList()
}

/**
 * 更改页容量事件
 * @param val 
 */
const handleSizeChange = (val: number) => {
  queryParams.size = val
  loadList()
}

/**
 * 换页事件
 * @param val 
 */
const handleCurrentChange = (val: number) => {
  queryParams.current = val
  loadList()
}

/**
 * 复选框事件
 * @param val 
 */
const handleSelectionChange = (val: Tag[]) => {
  multipleSelection.value = val.map(item => item.tagId)
  multipleSelectionName.value = val.map(item => item.name)
}

/**
 * 重置搜索
 */
const reset = () => {
  form.name = ''
}

/**
 * 添加数据
 */
const handleAdd = () => {
  reset()
  title.value = '新增'
  visible.value = true
}

/**
 * 删除数据
 * @param tag 
 */
const handleDelete = async (tag: Tag | any) => {
  let isDelete;
  let ids = tag.tagId || multipleSelection.value
  let message = tag.name ? tag.name : String(multipleSelectionName.value)
  if (ids && ids.length !== 0) {
    try {
      await ElMessageBox.confirm(
        '是否要删除:' + message,
        '提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
      isDelete = await deleteByIds(ids)
      if (isDelete) {
        loadList()
        ElMessage({
          message: '删除成功',
          type: 'success',
        })
      } else {
        ElMessage({
          message: '请选择删除数据',
          type: 'error',
        })
      }
    } catch (err) {

    }

  } else {
    ElMessage({
      message: '请选择删除数据',
      type: 'error',
    })
  }
}

/**
 * 提交表单
 */
const submitForm = async () => {
  const ok = await add(form)
  loadList()
  visible.value = false
  if (ok) {
    ElMessage({
      message: '添加成功',
      type: 'success',
    })
  }
}
</script>